/*
 * @Author: nainaiyi s15037075861@163.com
 * @Date: 2022-09-05 15:32:53
 * @LastEditors: nainaiyi s15037075861@163.com
 * @LastEditTime: 2022-09-05 16:05:22
 * @FilePath: \shopapp\src\store\hook.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { useMemo } from "react";
import { RootState } from "./index";
import { useSelector } from "react-redux";

export function UserShop() {
    const shopList = useSelector((state: RootState) => state.shopReducer.shopList)
    const allPrice = useMemo(() => {
        return shopList.reduce((p, n) => {
            return n.isChecked ? p + n.count * n.price : p
        }, 0)
    }, [shopList])
    const allCount = useMemo(() => {
        return shopList.reduce((p, n) => {
            return n.isChecked ? p + n.count : p
        }, 0)
    }, [shopList])
    const allChecked = useMemo(() => {
        return shopList.length ? shopList.every(v => v.isChecked === true) : false
    }, [shopList])
    return { allPrice, allCount, allChecked }
}